package fr.sharpe.app.earthvisualizationproject.model;

import fr.sharpe.app.earthvisualizationproject.controller.CacheLayerController;
import fr.sharpe.app.earthvisualizationproject.ui.Earth;
import gov.nasa.worldwind.layers.Layer;

import java.util.HashMap;

import javax.swing.table.AbstractTableModel;

public class TableLayerModel extends AbstractTableModel{

	private boolean DEBUG = true;
	private CacheLayerController cacheOfLayers;
	private Earth earth = Earth.getInstance();
	
	private String[] columnNames = {"Layer","Count","Display"};

	public TableLayerModel() {
		this.cacheOfLayers = CacheLayerController.getInstance();
		
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return (int) cacheOfLayers.numberOfLayers();
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		return getData()[row][col];
	}
	
	private Object[][] getData()
	{
		Object[][] data = new Object[getRowCount()][3];
		int index = 0;
		for(String s:this.cacheOfLayers.keySet())
		{
			data[index][0] = s;
			if(this.cacheOfLayers.getLayer(s) == null)
			{
				data[index][1] = "SYSTEM";
			}
			else
			{
				data[index][1] = this.cacheOfLayers.getLayer(s).getOriginalObjects().size();
			}
			data[index][2] = earth.getLayer(s).isEnabled();
			index++;
		}
		return data;

	}
	

	/*
	 * JTable uses this method to determine the default renderer/
	 * editor for each cell.  If we didn't implement this method,
	 * then the last column would contain text ("true"/"false"),
	 * rather than a check box.
	 */
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	/*
	 * Don't need to implement this method unless your table's
	 * editable.
	 */
	public boolean isCellEditable(int row, int col) {
		//Note that the data/cell address is constant,
		//no matter where the cell appears onscreen.
//		if (col < 1) {
//			return false;
//		} else {
//			return true;
//		}
		return true;
	}

	/*
	 * Don't need to implement this method unless your table's
	 * data can change.
	 */
	public void setValueAt(Object value, int row, int col) {
		if (DEBUG) {
			System.out.println("Setting value at " + row + "," + col
					+ " to " + value
					+ " (an instance of "
					+ value.getClass() + ")");
		}

		String layer = (String) getData()[row][0];
		
		earth.getLayer(layer).setEnabled(Boolean.parseBoolean(value.toString()));
		fireTableCellUpdated(row, col);

		if (DEBUG) {
			System.out.println("New value of data:");
			printDebugData();
		}
	}

	private void printDebugData() {
		int numRows = getRowCount();
		int numCols = getColumnCount();

		for (int i=0; i < numRows; i++) {
			System.out.print("    row " + i + ":");
			for (int j=0; j < numCols; j++) {
				System.out.print("  " + getData()[i][j]);
			}
			System.out.println();
		}
		System.out.println("--------------------------");
	}
}